﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Twos.Web.Service.Entities;

namespace Twos.Web.Service
{
   public class BaseService<T> where T:BaseEntity
    {


        private Model1 db;
        public BaseService(Model1 db)
        {
            this.db = db;
        }
        public bool Add(T entity)
        {
            db.Entry<T>(entity).State = EntityState.Added;
            return db.SaveChanges() > 0;
        }
        public bool Update(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;

        }
        /// <summary>
        ///删除 IsDel软删除 所以是修改EntityState.Modified;
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>

        public bool Delete(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;
        }



        /// <summary>
        /// 查整体
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> GetLists(Expression<Func<T, bool>> whereLambda)
        {
            return db.Set<T>().Where(whereLambda);
        }

        /// <summary>
        /// 查个体
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public T Get(Expression<Func<T, bool>> whereLambda)
        {
           return GetLists(whereLambda).FirstOrDefault();
        
        }
        /// <summary>
        /// 排序
        /// isAsc = true默认为正序排序  false 倒序
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="whereLambda"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetList<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy, bool isAsc = true)
        {
            if (isAsc)
            {
                
                return GetLists(whereLambda).OrderBy(orderBy);
            }
            else
            {
                //倒序
                return GetLists(whereLambda).OrderByDescending(orderBy);
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="start"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetPagedList<TKey>(int start, int pageSize, ref int rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy, bool isAsc = true)
        {
            //获取总记录数
            rowCount = GetLists(whereLambda).Count();

            //边界值判断
            start = start < 1 ? 0 : start;

            if (isAsc)
            {
                return GetLists(whereLambda).OrderBy(orderBy).Skip(start).Take(pageSize);
            }
            else
            {
                return GetLists(whereLambda).OrderByDescending(orderBy).Skip(start).Take(pageSize);
            }
        }
   

    }
}
